﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;
using System.Drawing;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class WidgetController : Controller
    {
        private IBusinessSystemInfo BusinessSystemInfoService;
        private IBusinessAvailability BusinessAvailabilityService;

        [InjectionConstructor]
        public WidgetController(
            IBusinessSystemInfo bsinfo,
            IBusinessAvailability bainfo)
        {
            BusinessSystemInfoService = bsinfo;
            BusinessAvailabilityService = bainfo;
        }

        //
        // GET: /Widget/

        public ActionResult BusinessInfo()
        {
            Thread.Sleep(2000);

            Wicresoft.UnifyShow.Portal.BusinessInfo bi = new BusinessInfo();
            bi.SystemInfo = BusinessSystemInfoService.GetSystemInfo();
            bi.VendorInfo = BusinessSystemInfoService.GetVendorInfo();
            bi.FinanceInfo = BusinessSystemInfoService.GetFinanceInfo();

            return Json(bi);
        }

        [OutputCache(CacheProfile = "Report",VaryByParam="bid;sid")]
        public ActionResult BusinessAvaliability(int bid,int? sid)
        {
            //Thread.Sleep(1000);
            List<BusinessAvailabilityModel> bas = new List<BusinessAvailabilityModel>();
            if (sid == null)
                bas = BusinessAvailabilityService.GetBusinessAvailability(bid.ToString(), new DateTime(2012, 7, 10), new DateTime(2012, 7, 20));
            else
                bas = BusinessAvailabilityService.GetBusinessAvailability(bid.ToString(),sid.ToString(),new DateTime(2012, 7, 10), new DateTime(2012, 7, 20));

            var group = bas.GroupBy(b=>b.SiteName);
            Series series1 = new Series();
            Chart colChart = new Chart();
            ChartArea chartArea1 = new ChartArea();
            chartArea1.Name = "Default";
            foreach (var k in group)
            {
                series1.Points.AddXY(k.Key, k.Average(b => b.Availability));
                
            }
            
            //chartArea1.AxisX.LabelAutoFitMaxFontSize = 5;
            //chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            //chartArea1.AxisX.LabelStyle.IsEndLabelVisible = true;
            //chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
            //chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            //chartArea1.AxisY.LabelAutoFitMaxFontSize = 8;
            //chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY.LabelStyle.Format = "0%";
            //chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            //chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            //chartArea1.BackColor = System.Drawing.Color.OldLace;
            //chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            //chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            //chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            
            //chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            //chartArea1.AxisX.LabelStyle.IntervalOffset = 1;
            //chartArea1.AxisX.LabelStyle.IntervalOffsetType = DateTimeIntervalType.Auto;
            //chartArea1.AxisX.LabelStyle.Interval = 2;
            //chartArea1.AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Auto;
            chartArea1.AxisX.Title = "业务可用度";   
         

            colChart.ChartAreas.Add(chartArea1);


            series1.BorderColor = System.Drawing.Color.FromArgb(180, 26, 59, 105);
            series1.ChartArea = "Default";
            series1.Legend = "Default";
            series1.Name = "Series1";
            series1.IsValueShownAsLabel = true;
            series1.LabelFormat = "0%";
            
            series1.XValueType = ChartValueType.String;

            colChart.Series.Add(series1);
            colChart.Size = new System.Drawing.Size(412, 296);

            using(MemoryStream ms = new MemoryStream())
            {
                colChart.SaveImage(ms, ChartImageFormat.Gif);
                return File(ms.ToArray(), "image/gif");
            }

            

        }
    }
}
